<?php
/**
	後臺登錄函數
*/
class LoginAction extends Action 
{
	
    /**
		定义空操作
	*/
	public function _empty()
	{
		
		$this->_pageNotFound();
	}
	
	/**
		登錄頁面
	*/
	public function index()
	{
		if (!( Session::is_set('admin_id') && (Session::get('admin_id') > 0)) ){
			if ($_POST['submitted'] && !empty($_POST['username']) && !empty($_POST['password'])) {

				$User = M('admin_user');
				$userName = $_POST['username'];
				$password = SHA1($_POST['password']);

				$result = $User->where("user_name='$userName' AND password='$password'")->find();

				if($result) {
					Session::set('admin_name',$result['user_name']);
					Session::set('admin_id',$result['admin_id']);
					//获取上次登录时间
					$lastInfo['last_login_ip'] = $result['last_login_ip'];
					$lastInfo['last_login_time'] = date('Y-m-d H:i:s',$result['last_login_time']);
					Session::set('last_login_ip', $lastInfo['last_login_ip']);
					Session::set('last_login_time', $lastInfo['last_login_time']);
					//记录此次登录者IP和登录时间
					Load('extend');
					$data['last_login_ip'] = get_client_ip();
					$data['last_login_time'] = time();
					$User->where("admin_id={$result['admin_id']}")->save($data);
					
					$this->redirect('index');
				} else {
					$this->redirect('login/index', '', 3, '密码不正确');
				}
			} else {
				$this->assign('title','login');
				$this->display();
			}
		} else {
			$this->redirect('index/index');
		}
	}
	
	/**
		退出登录
	*/
	public function logout()
	{
		Session::destroy();
		$this->redirect('index');
	}
	
}